package com.export.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import common.entity.PageInfo;
import common.exec.RsjIllegalArgueException;
import common.statusenum.CommonEnum;
import common.util.DateTimeFormatterUtil;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

/**
 * 报表查询参数
 *
 * @author yuanqinglong
 * @since 2021/7/21 16:51
 */
@Data
public class ReportParam {


    /**
     * 统计类型（1=年，2=月，3=日）
     */
    @NotBlank(message = "统计类型不可为空")
    private String statisticType;
    /**
     * 开始日期
     */
    @NotBlank(message = "统计开始时间不可为空")
    private String startDate;
    /**
     * 结束日期
     */
    @NotBlank(message = "统计结束时间不可为空")
    private String endDate;
    /**
     * 客户编号
     */
    @NotBlank(message = "客户编号不可为空", groups = CustId.class)
    private String custId;

    /**
     * 客户简称
     */
    private String custSname;
    /**
     * 账号标识
     */
    @NotBlank(message = "账号标识不可为空", groups = AutKeyid.class)
    private String autKeyid;
    /**
     * 接口编号
     */
    @NotBlank(message = "接口编号不可为空", groups = ProdUid.class)
    private String prodUid;
    /**
     * 接口分类
     */
    @NotBlank(message = "接口分类不可为空", groups = ProdType.class)
    private String prodTypeFid;
    /**
     * 分页
     */
    @JSONField(name = "_page_")
    @NotNull(message = "分页信息不可为空", groups = {Table.class})
    private PageInfo pageInfo;

    /**
     * 操作人id
     */
    private String operatorId;


    private Integer pageSize;

    private Integer pageOffset;

    public Integer getPageSize() {
        return pageInfo.getPageSize();
    }

    public Integer getPageOffset() {
        return pageInfo.getPageOffSet();
    }

    public LocalDate getStartDate() {
        return this.processingStartTime();
    }

    public LocalDate getEndDate() {
        return this.processingEndTime();
    }

    /**
     * 处理时间
     */
    private LocalDate processingStartTime() {
        switch (statisticType) {
            case CommonEnum.YEAR_CODE:
                return LocalDate.of(Integer.parseInt(startDate),1, 1);
            case CommonEnum.MONTH_CODE:
                String[] split = startDate.split("-");
                return LocalDate.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]), 1);
            case CommonEnum.DAY_CODE:
                return DateTimeFormatterUtil.stringDateToLocalDate(startDate);
            default:
                return null;
        }
    }

    /**
     * 处理时间
     */
    private LocalDate processingEndTime() {
        switch (statisticType) {
            case CommonEnum.YEAR_CODE:
                return LocalDate.of(Integer.parseInt(endDate), 12, 31);
            case CommonEnum.MONTH_CODE:
                String[] split = endDate.split("-");
                LocalDate end = LocalDate.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]), 1);
                return end.with(TemporalAdjusters.lastDayOfMonth());
            case CommonEnum.DAY_CODE:
                return DateTimeFormatterUtil.stringDateToLocalDate(endDate);
            default:
                return null;
        }
    }


    /**
     * 统计时间类型分组
     *
     * @return {@link String}
     */
    public String statisticalTimeTypeGrouping() {
        switch (statisticType) {
            case CommonEnum.YEAR_CODE:
                return "YYYY";
            case CommonEnum.MONTH_CODE:
                return "YYYY-MM";
            case CommonEnum.DAY_CODE:
                return "YYYY-MM-DD";
            default:
                throw new RsjIllegalArgueException("统计类型错误");
        }
    }


    /**
     * 处理操作员
     *
     * @param box 盒子
     */
    public void processingOperator(Box box) {
        MapNode user = box.getMapNode("$user");
        operatorId = user.getValue("User_Id");
    }




    /**
     * 得到原始的开始日期
     *
     * @return {@link String}
     */
    public String getOriginalStartDate() {
        return startDate;
    }

    /**
     * 得到原始的结束日期
     *
     * @return {@link String}
     */
    public String getOriginalEndDate() {
        return endDate;
    }

    public interface Table {
    }

    public interface AutKeyid {
    }

    public interface CustId {
    }

    public interface ProdUid {
    }

    public interface ProdType {
    }

}
